#!/usr/bin/env python2.5
# encoding: utf-8

if not globals().has_key("esda"):
    execfile("esda.py")
    sys.exit()

init()

import random

name = "Stream"

newStream(name)

for j in range(100):
    streams[name][50*j:50*j+50] = random.random()

def aggregationStartTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime

    aggregationStartTime = int(startTime / 200) * 200
    context['result'] = aggregationStartTime

def aggregationEndTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime

    context['result'] = max(aggregationStartTime + 200, int(endTime / 200) * 200)

def aggregationValueIterator(context, startTime, endTime, value):
    context['result'] = value + context['result'] if context['result'] != None else value

def aggregationGrouper(startTime, endTime, value):
    return int(startTime / 200)

newAggregationStream(
    "AggregationOfStream", "Stream",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator, aggregationGrouper)
